package com.springboot.news.controller;

import com.springboot.news.domain.dto.CategoryDTO;
import com.springboot.news.domain.po.Category;
import com.springboot.news.domain.po.Result;
import com.springboot.news.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping("/add")
    public Result add(@RequestBody @Validated(CategoryDTO.Add.class) Category category) {
//        String catoryName = category.getCategoryName();
//        String catoryAlias = category.getCategoryAlias();
//        if (!StringUtils.hasLength(catoryName) || !StringUtils.hasLength(catoryAlias)) {
//            return Result.error("不能为空");
//        }
        categoryService.add(category);
        return Result.success();
    }
    @GetMapping("/list")
    public Result<List<Category>> list() {
        List<Category> listData = categoryService.list();
        return Result.success(listData);
    }
    @GetMapping("/detail")
    public Result<Category> detail(@RequestParam Integer id) {
        Category category = categoryService.detail(id);
        return Result.success(category);
    }
    @DeleteMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        categoryService.delete(id);
        return Result.success();
    }
    @PostMapping("/update")
    public Result update(@RequestBody @Validated(CategoryDTO.Update.class) Category category) {
        categoryService.update(category);
        return Result.success();
    }
}
